package com.zretc.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import com.zretc.entity.CommentInfo;
import com.zretc.entity.CommentRelation;
import com.zretc.entity.OrderInfo;
import com.zretc.entity.UserInfo;
import com.zretc.service.OrderAndCollectionService;
import com.zretc.service.UserService;
import com.zretc.service.impl.OrderAndCollectionServiceImpl;
import com.zretc.service.impl.UserServiceImpl;

/**
 * 评论相关的请求与响应
 * @author liumeiqi777
 *
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserService userService =new UserServiceImpl(); 
    private OrderAndCollectionService orderAndCollectionService =new OrderAndCollectionServiceImpl(); 
    
    public CommentServlet() {
        super();
   
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求的参数
		
		String op = request.getParameter("op");
		if("addComment".equals(op)) {//对商品添加一条评论
			doAddComment(request,response);
		}else if("findComment".equals(op)) {
			doFindComment(request,response);
		}else if("findChildrenComment".equals(op)) {
			doFindChildrenComment(request,response);
		}else if("replyComment".equals(op)) {
			doReplyComment(request,response);
		}
	}
    private void doReplyComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	//获取父评论的评论id
		String parentId = request.getParameter("parentId");
		//获取当前的回复内容
		String commentContent =request.getParameter("commentContent");
		//获取当前用户的信息
		 HttpSession session = request.getSession();
		 UserInfo userInfo = (UserInfo) session.getAttribute("user");//获取session中的user对象
		//获取当前的日期时间
		 Date now=new Date();
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	     String commentTime=dateFormat.format(now);
	    //将评论人Id和回复时间结合作为评论Id
		 String commentId = userInfo.getUserId()+commentTime;
		//将评论时间作为评论关系id
		 String commentRelationId = commentTime;
		 //创建一个新的评论关系对象
		 CommentRelation commentRelation = new CommentRelation(commentRelationId, commentId, parentId);
		 //创建一个新的评论对象，包含评论关系
		 CommentInfo commentInfo = new CommentInfo(commentId, commentTime, commentContent, null, null, userInfo,commentRelation);
		 boolean saveState = userService.saveCommentInfoRelation(commentInfo);//调用回复方法
		 if(saveState) {
			 response.getWriter().write("回复成功");
		 }else {
			 response.getWriter().write("回复失败");
		 }
		
	}

//获取子评论的方法
	private void doFindChildrenComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String parentId = request.getParameter("parentId");
		List<Map<String, String>> findChildrenComments = userService.findChildrenComments(parentId);
		//使用Gson将返回值翻译成json字符串
		Gson gson = new Gson();
		String commentInfos = gson.toJson(findChildrenComments);
		//设置响应的类型为json字符串
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(commentInfos);
		
	}
//获取一级评论的方法
	private void doFindComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String roomId = request.getParameter("roomId");
		List<Map<String, String>> findCommentInfos = userService.findCommentInfos(roomId);
		//使用Gson将返回值翻译成json字符串
		Gson gson = new Gson();
		String commentInfos = gson.toJson(findCommentInfos);
		//设置响应的类型为json字符串
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(commentInfos);
	
	}
//添加一条一级评论的方法
	private void doAddComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("进行添加评论的方法");
		String commentScore = request.getParameter("score");
		String commentContent = request.getParameter("message");
		String orderId = request.getParameter("orderId");
		System.out.println("被评论的订单编号为"+orderId);
		//获取当前用户的信息
		 HttpSession session = request.getSession();
		 UserInfo userInfo = (UserInfo) session.getAttribute("user");//获取session中的user对象
		//获取当前的日期时间
		 Date now=new Date();
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	     String commentTime=dateFormat.format(now);
	    //将评论人Id和回复时间结合作为评论Id
		 String commentId = userInfo.getUserId()+commentTime;
		 //默认一级评论的评论关系id为1
		 CommentRelation commentRelation = new CommentRelation("1", null, null);
	     OrderInfo orderInfo =new OrderInfo(orderId, null, null, null, null, null);
		CommentInfo commentInfo = new CommentInfo(commentId, commentTime, commentContent, commentScore, orderInfo, userInfo, commentRelation);
	    boolean saveCommentInfoState = userService.saveCommentInfo(commentInfo);
	    if(saveCommentInfoState) {
			 response.sendRedirect("./front/index.jsp");
		 }else {
			 response.sendRedirect("./front/comment.jsp");
		 }
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
